package com.motionlog.activity;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.motionlog.R;
import com.motionlog.model.Database;

public class Settings extends Activity implements OnClickListener {

	private static final int DIALOG_BACKUP = 0;

	private Database mDb;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);

		mDb = Database.getInstance(this);

		TextView clearPassword = (TextView) findViewById(R.id.clear_password);
		clearPassword.setOnClickListener(this);
		TextView backup = (TextView) findViewById(R.id.backup);
		backup.setOnClickListener(this);
		TextView checkUpdate = (TextView) findViewById(R.id.check_update);
		checkUpdate.setOnClickListener(this);
		TextView about = (TextView) findViewById(R.id.about);
		about.setOnClickListener(this);
		TextView advice = (TextView) findViewById(R.id.advice);
		advice.setOnClickListener(this);
		TextView exit = (TextView) findViewById(R.id.exit);
		exit.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.clear_password:
			getSharedPreferences("settings", MODE_PRIVATE).edit().putBoolean(
					"password", false).commit();
			break;
		case R.id.backup:
			showDialog(DIALOG_BACKUP);
			break;
		case R.id.check_update:
			break;
		case R.id.about:
			break;
		case R.id.advice:
			break;
		case R.id.exit:
			finish();
			break;
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new Builder(this);
		switch (id) {
		case DIALOG_BACKUP:
			final CharSequence[] items = { "����", "��ԭ", "��ʼ��" };
			builder.setItems(items, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int item) {
					switch (item) {
					case 0:
						try {
							mDb.backupDatabase();
							Toast.makeText(Settings.this, "���ݳɹ�",
									Toast.LENGTH_LONG).show();
						} catch (IOException e) {
							Toast.makeText(Settings.this, "����ʧ��",
									Toast.LENGTH_LONG).show();
							e.printStackTrace();
						}
						break;
					case 1:
						try {
							mDb.restoreDatabase();
							Toast.makeText(Settings.this, "��ԭ�ɹ�",
									Toast.LENGTH_LONG).show();
						} catch (IOException e) {
							Toast.makeText(Settings.this, "��ԭʧ��",
									Toast.LENGTH_LONG).show();
							e.printStackTrace();
						}
						break;
					case 2:
						try {
							Toast.makeText(Settings.this, "��ʼ���ɹ�",
									Toast.LENGTH_LONG).show();
							mDb.copyDataBase();
						} catch (IOException e) {
							Toast.makeText(Settings.this, "��ʼ��ʧ��",
									Toast.LENGTH_LONG).show();
							e.printStackTrace();
						}
						break;
					}
				}
			});
			break;
		}
		return builder.show();
	}
}
